package router

import (
	"gin-gateway/core/controller"
	"gin-gateway/middleware"
	"gin-gateway/middleware/http_proxy_middleware"
	"github.com/gin-gonic/gin"
)

func InitProxyRouter() *gin.Engine {
	//todo 优化点1
	//router := gin.Default()
	router := gin.New()

	// pprof.Register(router)
	router.Use(middleware.RecoveryMiddleware(), middleware.RequestLog())
	router.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})
	router.Static("/dist", "./dist")

	// 根据 appId 和 secret 来生成 jwt token，供给租户使用
	controller.OAuthRegister(router.Group("/oauth", middleware.TranslationMiddleware()))
	router.Use(
		//http_proxy_middleware.HTTPAccessModeMiddleware(), // 根据数据库中指定的匹配模式（域名匹配或前缀匹配），找到合适的配置参数
		//http_proxy_middleware.HTTPFlowCountMiddleware(),         // 用自定义的流量统计功能
		//http_proxy_middleware.HTTPFlowLimitMiddleware(),         // 用 rate 包做的限流
		http_proxy_middleware.HTTPWhiteAndBlackListMiddleware(), // 黑/白名单 黑名单先校验于 白名单
		// http_proxy_middleware.HTTPCircuitMiddleware(),	// 熔断降级
		http_proxy_middleware.HTTPJwtAuthTokenMiddleware(),   // 租户使用 jwt
		http_proxy_middleware.HTTPJwtFlowCountMiddleware(),   // 用jwt流量统计功能
		http_proxy_middleware.HTTPJwtFlowLimitMiddleware(),   // jwt限流功能 。用 rate 包做
		http_proxy_middleware.HTTPHeaderTransferMiddleware(), // Header头内容重写
		http_proxy_middleware.HTTPStripUrLMiddleware(),       // URL路径去除指定字符串。如去除接入前缀 127.0.0.1:8080/test_http_string/abbb  127.0.0.1:2004/abbb
		http_proxy_middleware.HTTPUrlRewriteMiddleware(),     // url重写 匹配路径。^/test_http_service/abbb(.*) /test_http_service/baaa$1 // 将匹配到的“/test_http_service/abbb” 放入转为“/test_http_service/baaa
		http_proxy_middleware.HTTPReverseProxyMiddleware(),   // 反向代理+负载均衡
	)
	return router
}
